<script setup lang="ts">
// 导入组件
import XtxSwiper from './components/XtxSwiper.vue'
import XtxNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import XtxGuess from './components/XtxGuess.vue'
import { ref } from 'vue'
import type { XtxGuessInstance } from '@/types/component'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()
// 滚动触底事件
const onScrolltolower = () => {
  guessRef.value?.getMore()
}
// 下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData() // 加载数据
  await Promise.all([getHomeBannerAPI(), getHomeCategoryAPI(), getHomeHotAPI()]) // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <!-- 滚动容器 -->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    class="scroll-view"
    scroll-y
    @scrolltolower="onScrolltolower"
  >
    <!-- 自定义导航条 -->
    <XtxNavbar />
    <!-- 轮播图 -->
    <XtxSwiper />
    <!-- 前台类目 -->
    <CategoryPanel />
    <!-- 热门推荐 -->
    <HotPanel />
    <!-- 猜你喜欢 -->
    <XtxGuess ref="guessRef" />
  </scroll-view>
</template>

<style lang="scss">
// 添加滚动视图样式
.scroll-view {
  height: 100vh;
  background-color: #f7f7f7;
}
</style>
